﻿using Super.Hmi.Models.User;
using System.Collections.ObjectModel;

namespace Super.Hmi.Utils
{
    /// <summary>
    /// 通用方法封装
    /// </summary>
    public static class CommonUtils
    {
        /// <summary>
        /// 当使用用户下拉控件时，过滤用户列表
        /// </summary>
        /// <param name="sourceList">源数据List</param>
        /// <param name="searchName">关键字</param>
        /// <returns></returns>
        public static IEnumerable<UserModel> FilterUserList(IEnumerable<UserModel> sourceList, string searchName)
        {
            if (searchName.Split("]").Length > 1 && searchName.Split("[").Length > 1)
            {
                string userId = searchName.Split("[")[1].Split("]")[0];
                string userName = searchName.Split("]")[1];
                return new ObservableCollection<UserModel>(sourceList.Where(u => u.UserId.ToString() == userId && u.UserName == userName).ToList());
            }
            else if (searchName.Contains("]") || searchName.Contains("["))
            {
                return sourceList;
            }
            return new ObservableCollection<UserModel>(sourceList.Where(u => u.UserId.ToString().Contains(searchName) || u.UserName.Contains(searchName)).ToList());
        }
    }
}
